package org.dromara.web.plugins.pay;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class PayProviderFuhai implements PayProvider {

    @Override
    public String preCreateOrder() {
        Map<String, Object> signMap = new HashMap<>();
        signMap.put("mchId", "1-89");
        signMap.put("productCode", "118");
        signMap.put("amount", 100);
        signMap.put("merchantOrderId", "CS202301071706565398");
        signMap.put("timestamp", DateUtil.currentSeconds());
        signMap.put("nonce", "202301071706569735");
        signMap.put("notifyUrl", "http://hh.ttpay02.xyz:8085/test/test");
        signMap.put("returnUrl", "https://www.google.com");

        Set<Map.Entry<String, Object>> entries = signMap.entrySet();
        Iterator<Map.Entry<String, Object>> iterator = entries.iterator();
        List<String> values = new ArrayList<>();
        while (iterator.hasNext()) {
            Map.Entry<String, Object> entry = iterator.next();
            String k = String.valueOf(entry.getKey());
            String v = String.valueOf(entry.getValue());
            if (ObjectUtil.isNotEmpty(k) && ObjectUtil.isNotEmpty(v)) {
                values.add(k + "=" + v);
            }
        }
        values.add("key=f285b0204cb64483800e5071b3af5de6");
        String str = StrUtil.join("&", values);
        String sign = SecureUtil.md5(str);
        signMap.put("sign", sign);
        return HttpUtil.createPost("").form(signMap).contentType("application/json").body(JSONUtil.toJsonStr(signMap)).execute().body();
    }

    @Override
    public String notifyResult() {
        return null;
    }
}
